package gui;



import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import schedule.Appointment;
import schedule.AppointmentList;

public class Schedule_window extends JFrame implements ActionListener{
	AppointmentList list = new AppointmentList();
	Appointment_panel appointments = new Appointment_panel(list);
	
	JButton enter = new JButton("Enter");
	JButton show = new JButton("Show");
	
	JTextField titletf = new JTextField(15);
	JTextField datetf = new JTextField(15);
	JTextField descriptiontf = new JTextField(15);
	JTextField endtf = new JTextField(15);
	
	JPanel content = new JPanel(new BorderLayout());
	
	JLabel message = new JLabel();
	
	JTextArea output = new JTextArea(3, 3);

	
	public Schedule_window(){
		setTitle("Schedule");
		setLayout(new BorderLayout());
		add(appointments, BorderLayout.EAST);
		
		initMainScreen();
	}
	
	public void initMainScreen(){
		
		JLabel title = new JLabel("Title: ");
		JLabel date = new JLabel("Startdate: ");
		JLabel end = new JLabel("Enddate: ");
		JLabel description = new JLabel("Description");
		
		enter.addActionListener(this);
		show.addActionListener(this);
		
		JPanel main = new JPanel(new GridLayout(5,2));
		main.add(title);
		main.add(titletf);
		main.add(date);
		main.add(datetf);
		main.add(end);
		main.add(endtf);
		main.add(description);
		main.add(descriptiontf);
		main.add(enter);
		main.add(show);
		content.add(main, BorderLayout.NORTH);
		content.add(message, BorderLayout.SOUTH);
		output.setEditable(false);
		content.add(new JScrollPane(output), BorderLayout.CENTER);
		add(content, BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == enter){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hhmm");
			try{
				Date convertedDate = (Date) sdf.parse(datetf.getText());
				Date convertedEndDate = (Date) sdf.parse(endtf.getText());
				list.add(new Appointment(titletf.getText(),convertedDate, convertedEndDate, descriptiontf.getText()));
//				System.out.println("Funktioniert");
				message.setText("appointment entered");
			}catch(ParseException pe){
//				System.err.println("Exception: wrong date format (YYYYMMDD)");
				message.setText("wrong date format:yyyyMMdd hhmm");
			}catch(Exception ex){
				message.setText(ex.getMessage());
			}
			
			appointments.writeAppointments();
		}
		
		if(e.getSource() == show){
			output.setText("");
			List<Appointment> appointments = new AppointmentList();
			try{
				appointments = list.searchTitle(titletf.getText());
			} catch( Exception ex ){
				message.setText(ex.getMessage());
			}
			for(int i = 0;i< appointments.size();i++){
				output.append(appointments.get(i).getTitle() + "\nStart: " + appointments.get(i).getDate().toString() + "\nEnd:  " + appointments.get(i).getEnd() + "\n" + appointments.get(i).getDescription()+"\n");
			}
		}
	}
}
